#include <windows.h>
#include <cstring>

LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

HWND hWnd;
HWND hwndHoursLabel;
HWND hwndHoursEdit;
HWND hwndWageLabel;
HWND hwndWageEdit;
HWND hwndButton;
HWND hwndGrossLabel;
HWND hwndGrossAmount;
HWND hwndWithheldLabel;
HWND hwndWithheldAmount;
HWND hwndNetLabel;
HWND hwndNetAmount;
char szHoursResult[10];
char szWageResult[10];
char szResult[10];
double dHoursResult;
double dWageResult;
double dRegularPay;
double dOvertimePay;
double dGrossPay;
double dNetPay;
double dAmountWithheld;


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
	LPSTR lpCmdLine, int nCmdShow) {

	WNDCLASS wc;

	wc.lpszClassName = "GuessNumber";
	wc.lpfnWndProc = MainWndProc;
	wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
	wc.lpszMenuName = "";
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	RegisterClass( &wc );

	hWnd = CreateWindow(
		"GuessNumber", "Guess Number",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		225,
		225,
		NULL,
		NULL,
		hInstance,
		NULL);

	hwndHoursLabel = CreateWindow( 
		"Static", 
		"Hours worked:", 
		WS_VISIBLE | WS_CHILD,
		10,
		10, 
		100, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndHoursEdit = CreateWindow( 
		"EDIT", 
		NULL, 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
		110,
		10, 
		90, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndWageLabel = CreateWindow( 
		"Static", 
		"Hourly wage:", 
		WS_VISIBLE | WS_CHILD,
		10,
		35, 
		100, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndWageEdit = CreateWindow( 
		"EDIT", 
		NULL, 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
		110,
		35, 
		90, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndButton = CreateWindow( 
		"BUTTON",
		"Calc Pay", 
		WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
		10,
		60, 
		190, 
		35, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	hwndGrossLabel = CreateWindow( 
		"Static", 
		"Gross Pay: ", 
		WS_VISIBLE | WS_CHILD,
		10,
		105, 
		100, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndGrossAmount = CreateWindow( 
		"Static", 
		NULL, 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
		110,
		105, 
		90, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndWithheldLabel = CreateWindow( 
		"Static", 
		"Taxes (15%):", 
		WS_VISIBLE | WS_CHILD,
		10,
		130, 
		100, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndWithheldAmount = CreateWindow( 
		"Static", 
		NULL, 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
		110,
		130, 
		90, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndNetLabel = CreateWindow( 
		"Static", 
		"Net pay:", 
		WS_VISIBLE | WS_CHILD,
		10,
		155, 
		100, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndNetAmount = CreateWindow( 
		"Static", 
		NULL, 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
		110,
		155, 
		90, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	ShowWindow(hWnd, nCmdShow);

	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0 )) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}


LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, 
	WPARAM wParam, LPARAM lParam) {
	HWND hwndCtl = (HWND) lParam;
	switch (msg) {
		case WM_COMMAND:
			switch (wParam) {
				case BN_CLICKED:
						GetWindowText(hwndHoursEdit, szHoursResult, sizeof(szHoursResult));
						GetWindowText(hwndWageEdit, szWageResult, sizeof(szWageResult));
						dHoursResult = atof(szHoursResult);
						dWageResult = atof(szWageResult);
						if (dHoursResult <= 40)
							dGrossPay = dHoursResult * dWageResult;
						else {
							dRegularPay = 40 * dWageResult;
							dOvertimePay = 
                                                        (dHoursResult - 40) * (dWageResult * 1.5);
							dGrossPay = dRegularPay + dOvertimePay;
						}
						dAmountWithheld = dGrossPay * .15;
						dNetPay = dGrossPay - dAmountWithheld;
						_gcvt(dGrossPay, 10, szResult);
						SetWindowText(hwndGrossAmount, szResult);
						_gcvt(dNetPay, 10, szResult);
						SetWindowText(hwndWithheldAmount, szResult);
						_gcvt(dAmountWithheld, 10, szResult);
						SetWindowText(hwndNetAmount, szResult);
			}
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}
	